import axios from 'axios'
import qs from 'qs'
import myStore from '@/pages/store'
export default {
    install(Vue, options) {
        var apiRoot = process.env.API_ROOT;
        Vue.prototype.G_getApiRoot = function () {
            return apiRoot;
        }

        Vue.prototype.$http = axios;
        var sid = null;
        Vue.prototype.G_getSid = function () {
            return sid;
        }
        Vue.prototype.G_setSid = function (s) {
            return sid = s;
        }
        Vue.prototype.G_isLoged = function (s) {
            return sid !== null;
        }
        var accounts = null;
        Vue.prototype.G_getAccounts = function () {
            return accounts;
        }
        Vue.prototype.G_setAccounts = function (a) {
            return accounts = a;
        }
        Vue.prototype.G_encodeUrl = function (url, sessionFlag = true) {
            if (url && sessionFlag === true) {
                return Vue.prototype.G_getApiRoot() + url + "?" + Vue.prototype.G_getSid();
            } else if (url && sessionFlag === false) {
                return Vue.prototype.G_getApiRoot() + url;
            }
            return url;
        }

        //改post编码方式为原始方式
        axios.interceptors.request.use((config) => {
            // config.data = qs.stringify(config.data);
            config.data = config.data;

            return config;
        }, function (error) {
            return Promise.reject(error);
        });

        //loading框通讯bus
        var loadingHub = new Vue();
        Vue.prototype.G_getLoadingHub = function () {
            return loadingHub;
        }

        axios.interceptors.request.use((config) => {
            loadingHub.$emit('show'); //Hub触发show事件
            return config;
        }, function (error) {
            loadingHub.$emit('hide');
            alert("请求失败!");
            return Promise.reject(error);
        });

        axios.interceptors.response.use(function (response) {
            // Do something with response data
            loadingHub.$emit('hide');
            return response;
        }, function (error) {
            // Do something with response error
            loadingHub.$emit('hide');
            // alert("响应失败!");
            console.log("响应失败!");
            return Promise.reject(error);
        });

        Vue.prototype.G_showMsg = function (title, content, callback) {
            loadingHub.$emit('showModal', { 'title': title, 'content': content, 'callback': callback });
        }
        Vue.prototype.G_wyj = 'wyjwyj'
        Vue.prototype.G_showCheckCode = function () {
            loadingHub.$emit('showCheckCodeModal');
        }


        Vue.prototype.G_getUrlKey = function (name) { return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1]) || null; }

        var productInfo = null;
        Vue.prototype.G_setProductInfo = function (param) {
            productInfo = param;
        }
        Vue.prototype.G_getProductInfo = function () {
            return productInfo;
        }

        Vue.prototype.G_isValNullUndefinedEmpty = function (param) {
            if (typeof param === "undefined" || param === null || param === "") {
                return true;
            } else {
                return false;
            }
        }

    }

}
